package day_6_27;

public class Main3 {
    /**
     * 59. 螺旋矩阵 II
     * https://leetcode.cn/problems/spiral-matrix-ii/description/
     */
    public int[][] generateMatrix(int n) {
        int left = 0,right = n - 1;
        int top = 0,button = n - 1;
        int[][] ret = new int[n][n];
        int x = 1;
        int len = n * n;

        while(x <= len) {
            for(int i = left;i <= right;i++) {
                ret[top][i] = x++;
            }
            top++;

            for(int i = top;i <= button;i++) {
                ret[i][right] = x++;
            }
            right--;

            for(int i = right;i >= left;i--) {
                ret[button][i] = x++;
            }
            button--;

            for(int i = button;i >= top;i--) {
                ret[i][left] = x++;
            }
            left++;
        }
        return ret;
    }
}
